package com.it.xiaotao.io.echo;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class EchoServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(9999);
        System.out.println("等待客户端连接...");
        Socket client = server.accept();
        Scanner scan = new Scanner(client.getInputStream());
        scan.useDelimiter("\n");
        PrintStream out = new PrintStream(client.getOutputStream());
        boolean flag = true;
        while (flag) {
            if (scan.hasNext()) {
                String val = scan.next().trim();
                if ("byebye".equalsIgnoreCase(val)) {
                    out.println("ByeByeBye");
                    flag = false;
                } else {
                    out.println("【ECHO】" + val);
                }
            }
        }
        scan.close();
        out.close();
        client.close();
        server.close();
    }
}
